#import "SEGTopicPicView.h"

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif


#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

/**************************SEGTopicPicView************************/
@implementation SEGTopicPicView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kTopicPicViewHeight;
    }
    self = [super initWithFrame:frame];

    [self addSubview:self.topicTitleLabel];
//    [self addSubview:self.picView];
    [self addSubview:self.morePicture];
    
    self.userInteractionEnabled = YES;
//    __weak typeof(self) _self = self;
//    self.picView.userInteractionEnabled = YES;
//    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
//        if (_self.delegate && [_self.delegate respondsToSelector:@selector(topicPicViewPicTap:imageView:)]) {
//            [_self.delegate topicPicViewPicTap:_self.layout.data.picUrlStrArray imageView:self.picView.contentImageView];
//        }
//    }];
//    [self.picView addGestureRecognizer:tap2];
    
   // //if (kDebug)self.backgroundColor = kSegRandomColor;
    return self;
}

-(void)setWithLayout:(SEGTopicPicViewLayout *)layout{
    self.height = layout.totalHeight;
    _layout = layout;
    
    if (self.height <= 0) return;
    
    self.topicTitleLabel.textLayout = layout.topicTitleLabelTextLayout;
//    [self.picView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,_layout.data.picUrlString]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];

//    self.picView.delegate = self;

//    [self.picView setWithLayout:_layout.autoHeightImageViewLayout];
    

    
    
    [self setupUI];
}


/// 设置评论图片
- (void)seg_setImagesWithPicArray:(NSArray *)picArray
{
    
    [self.morePicture removeAllSubviews];
        
    CGFloat imageHeight = _layout.imageHeight;
    NSUInteger countInRow;//一行最大图片个数
    if (picArray.count == 1) {
        countInRow = 1;
        imageHeight = 170 * kScaleWidth;
    }else if (picArray.count == 4) {
        countInRow = 2;
    }else {
        countInRow = 3;
    }
    [picArray enumerateObjectsUsingBlock:^(NSString *imageString, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSURL *url = [NSURL URLWithString:imageString];
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.layer.masksToBounds = YES;
        [imageView setImageWithURL:url placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
        imageView.tag = 10 + idx;
        imageView.userInteractionEnabled = YES;
        __weak typeof(self) _self = self;
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(topicPicViewPicTap:imageView:)]) {
                [_self.delegate topicPicViewPicTap:_self.layout.data.picUrlStrArray imageView:(UIImageView *)sender.view];
            }
        }];
        [imageView addGestureRecognizer:tap2];
        
        [self.morePicture addSubview:imageView];
        
        CGFloat leftPadding = (idx % countInRow) * (imageHeight + _layout.imagePadding);
        CGFloat topPadding = (idx / countInRow) * (imageHeight + _layout.imagePadding);
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(imageHeight);
            make.top.equalTo(self.morePicture).offset(topPadding).priorityHigh();
            make.left.equalTo(self.morePicture).offset(leftPadding);
        }];
    }];
}


//界面布局
-(void)setupUI{
    [self.morePicture mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.top.mas_equalTo(self).offset(9);
        make.height.mas_equalTo(_layout.totalImageHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
    
    [self seg_setImagesWithPicArray:_layout.data.picUrlStrArray];
    
    self.topicTitleLabel.backgroundColor = kColor_L3;
    self.topicTitleLabel.layer.cornerRadius = 11.5;
    self.topicTitleLabel.layer.masksToBounds = YES;
    [self.topicTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self.morePicture.mas_bottom).offset(10);
        make.height.mas_equalTo(23);
        make.width.mas_equalTo(_layout.data.topicTitleWidth);
    }];

    self.topicTitleLabel.textAlignment = NSTextAlignmentLeft;
    self.topicTitleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
}

#pragma mark - getter
-(SEGLabel *)topicTitleLabel{
    if (!_topicTitleLabel) {
        _topicTitleLabel = [[SEGLabel alloc]init];
        _topicTitleLabel.userInteractionEnabled = YES;
        _topicTitleLabel.textAlignment = NSTextAlignmentLeft;
        _topicTitleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _topicTitleLabel.displaysAsynchronously = YES;
        _topicTitleLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _topicTitleLabel.fadeOnHighlight = NO;
        _topicTitleLabel.numberOfLines = 1;
        _topicTitleLabel.fadeOnAsynchronouslyDisplay = NO;
        _topicTitleLabel.fadeOnAsynchronouslyDisplay = NO;
        _topicTitleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
//        _topicTitleLabel.backgroundColor = kSegRandomColor;
        
        _topicTitleLabel.userInteractionEnabled = YES;
        __weak typeof(self) _self = self;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(topicPicViewTitleLabel:)]) {
                [_self.delegate topicPicViewTitleLabel:_topicTitleLabel];
            }
        }];
        [_topicTitleLabel addGestureRecognizer:tap];
    }
    return _topicTitleLabel;
}


//-(SEGAutoHeightImageView *)picView{
//    if (!_picView) {
//        _picView = [[SEGAutoHeightImageView alloc]init];
//        _picView.userInteractionEnabled = YES;
//       // //if (kDebug)_picView.backgroundColor = kSegRandomColor;
//    }
//    return _picView;
//}

-(UIView *)morePicture{
    if (!_morePicture){
        _morePicture = [[UIView alloc]init];
        //if (kDebug)_morePicture.backgroundColor = kSegRandomColor;
    }
    return _morePicture;
}


@end
